package com.bdqn.oa.dao.impl;

import com.bdqn.oa.dao.VoteoptionDao;
import com.bdqn.oa.entity.VoteOption;
import com.bdqn.oa.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author qiaolu
 * @date 2022/7/20 16:47
 */
public class VoteoptionDaolml implements VoteoptionDao {
    private QueryRunner queryRunner= new QueryRunner();
    /**
     * 通过VoteOptionid查询
     *
     * @param VoteOptionId
     * @return
     * @throws SQLException
     */
    @Override
    public VoteOption getbyidVoteOption(Integer VoteOptionId) throws SQLException {
        VoteOption voteOption = null;
        try {
            String sql = "select * from vote_option where id = ?";
            voteOption = queryRunner.query(DruidUtils.getConnection(),sql,new BeanHandler<VoteOption>(VoteOption.class),VoteOptionId);
        }catch (Exception e){
            e.printStackTrace();
        }

        return voteOption;
    }

    /**
     * 查询全部投票选项表
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<VoteOption> listAllvoteoption() throws SQLException {
        List<VoteOption> workList = null;
        try {
            String sql = "select * from vote_option";
            workList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<VoteOption>(VoteOption.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        return workList;
    }

    /**
     * 增加选项投票
     *
     * @param voteOption
     * @return
     * @throws SQLException
     */
    @Override
    public int saveVoteOption(VoteOption voteOption) throws SQLException {
        String sql = "insert into vote_option(vote_id,name,num)values(?,?,?)";
        return queryRunner.update(DruidUtils.getConnection(),sql,voteOption.getVoteId(),voteOption.getName(),voteOption.getNum());
    }

    /**
     * 修改投票选项
     *
     * @param voteOption
     * @return
     * @throws SQLException
     */
    @Override
    public int updateVoteOption(VoteOption voteOption) throws SQLException {
        String sql ="update vote_option set vote_id=?,name=?,num=? where id= ?";
        return queryRunner.update(DruidUtils.getConnection(),sql,voteOption.getVoteId(),voteOption.getName(),voteOption.getNum());
    }
}
